// pages/user/bank-card-add/bank-card-add.js
let app = getApp();
import Toast from '../../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
import Dialog from '../../../miniprogram/miniprogram_npm/vant-weapp/dialog/dialog'
const bankJson = require('../../../utils/bankJson/bank.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: '',
    toBankShow: false,//控制所属银行弹框
    toBankColumns: [],//所属银行数据
    name: '',
    idCard: '',
    idBank: '',
    mobile: '',
    toBank: '',//银行卡号实际属于银行
    bankList: bankJson.bank,//银行配置文件
    cardData: '',
    cdId: '',//银行卡id，修改使用
    bankId: '',//银行卡所属银行缩写
  },

  //删除
  delSubmit(){
    let _this = this;
    Dialog.confirm({
      message: '是否删除银行卡？'
    }).then(()=>{
      app.requestUrl({
        method: 'POST',
        url: '/wxapi/bank/addcard',
        data: {
          cdId: _this.data.cdId,
          isDelete: 1
        },
      }).then(res =>{
        Toast('删除成功');
        wx.navigateBack({
          detail: 1
        })
      })
    })
  },

  //保存
  saveSubmit(){
    let _this = this;

    if(!_this.data.name){
      Toast('请填写持卡人姓名'); return;
    } else if(!_this.data.idCard ){
      Toast('请填写身份证'); return;
    } else if(!_this.data.idBank){
      Toast('请填写银行卡卡号'); return;
    } else if(!_this.data.toBank){
      Toast('验证银行卡号错误，请核对后重试'); return;
    } else if(!_this.data.mobile){
      Toast('请填写银行卡预留电话'); return;
    }

    //保存
    app.requestUrl({
      method: 'POST',
      url: '/wxapi/bank/addcard',
      data:{
        mid: _this.data.user.id,
        username: _this.data.name,
        cardId: _this.data.idCard,
        cardPhone: _this.data.mobile,
        bbName: _this.data.toBank,
        tbName: _this.data.toBank,
        tbCode: '',
        bbCode: '',
        centerText_branch: '',
        bankId: _this.data.bankId,
        cardType: '',
        cdId: _this.data.cdId,
        cardNo: _this.data.idBank,
      }
    }).then(res =>{
      wx.navigateBack({
        delta: 1
      })
    })

  },

  //根据银行卡号得到所属银行
  judgeBank(){
    let _this = this;
    wx.request({
      method: 'POST',
      url: 'https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardBinCheck=true&cardNo=' + _this.data.idBank,
      dataType: 'json',
      success: function (res) {
        if(res.data.validated ){
            _this.setData({
              toBank: _this.data.bankList[res.data.bank],
              bankId: res.data.bank,
            })
        } else {
          _this.setData({
            toBank: ''
          })
//          Toast('验证银行卡号错误，请核对后重试');
        }
      },
      fail: function (err) {
        Dialog.alert({
          message: '验证银行卡号错误，请核对后重试'
        });
      }
    })
  },

  toBankConfirm(event){
    let _this = this;
    _this.toBankMonitor();
    _this.setData({
      toBank: event.detail.value
    })
  },
  toBankMonitor(){
    let _this = this;
    _this.setData({
      toBankShow: !_this.data.toBankShow
    })
  },


  nameBlur(event){
    this.setData({name: event.detail.value})
  },
  idCardBlur(event){
    this.setData({idCard: event.detail.value})
  },
  idBankBlur(event){
    this.setData({idBank: event.detail.value.replace(/\s*/g, "")})
    this.judgeBank();
  },
  mobileBlur(event){
    this.setData({mobile: event.detail.value})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (event) {
    let _this = this;
    _this.setData({
      user: app.getUser(),
      mobile: app.getUser().mobile,
      cardData: event.cardData!='undefined' ? JSON.parse(event.cardData) : ''
    })

    console.log(_this.data.cardData)
    if(_this.data.cardData){
      _this.setData({
        name: _this.data.cardData.nickname,
        idCard: _this.data.cardData.cardId,
        idBank: _this.data.cardData.cardNo,
        mobile: _this.data.cardData.cardPhone,
        toBank: _this.data.cardData.bbName,
        cdId:  _this.data.cardData.cdId,
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})